##### Check for Becoming normal

library(haven)
library(lme4)
library(texreg)
library(sjPlot)
library(sjstats)
library(fixest)
library(ggplot2)
library(ggeffects)
library(tidyverse)

load("Data/EP_debates_07062023.Rdata")

########## 

levels(EP_debates$EU_party)
levels(EP_debates$EU_party) <- c("NGL", "EFD/EFDD", "ECR",
                                 "EDD", "ALDE",  "EPP",
                                 "Greens","S&D", "ID",
                                 "No Group", "UEN", "EFD/EFDD", 
                                 "NGL", "Greens", "No Group")
levels(EP_debates$EU_party)

### BEGIN BY CENTERING AND SCALING EVERYTHING

EP_debates$radical <- abs(EP_debates$lrgen - 5)
range(EP_debates$radical, na.rm = TRUE)

centFUN <- function(x) {
  x - mean(x, na.rm = TRUE)
}

result <- apply(EP_debates[,c(22:24, 27:71, 73:75, 77:107,117:118)], MARGIN = 2, FUN = centFUN)
result <- as.data.frame(result)


EP_eurobar_cent <- cbind(EP_debates[,-c(22:24, 27:71, 73:75, 77:107,117:118)], result)

rm(list=setdiff(ls(), "EP_eurobar_cent"))

EP_eurobar_cent$embed_dict_scale <- scale(EP_eurobar_cent$embed_dict)
EP_eurobar_cent$aut_dict_scale <- scale(EP_eurobar_cent$aut_dict)
EP_eurobar_cent$immi_dict_scale <- scale(EP_eurobar_cent$immi_dict)
EP_eurobar_cent$int_dict_scale <- scale(EP_eurobar_cent$int_dict)
EP_eurobar_cent$lrgen_scale <- scale(EP_eurobar_cent$lrgen)
EP_eurobar_cent$radical_scale <- scale(EP_eurobar_cent$radical)

EP_eurobar_cent$aut_sal <- scale(EP_eurobar_cent$mip_n_govdebt_ipol)
EP_eurobar_cent$immi_sal <- scale(EP_eurobar_cent$mip_n_immigration_ipol)
EP_eurobar_cent$eum_ipol <- scale(EP_eurobar_cent$eum_ipol)




######## FOCUS ON PARTIES THAT TRANSFORM ################

EP_eurobar_cent <- EP_eurobar_cent %>%
  group_by(party_id) %>%
  mutate(chall_change = mean(challenge))

EP_transition <- subset(EP_eurobar_cent, EP_eurobar_cent$chall_change > 0) ## take all that are not always dominant parties

EP_challengers <- subset(EP_transition, EP_transition$challenge == 1) ### look at speeches given as challengers

EP_challengers <- EP_challengers %>%
  group_by(party_id) %>%
  mutate(change = max(date))

EP_challengers$running <- EP_challengers$date - EP_challengers$change
range(EP_challengers$running)

EP_dominants <- subset(EP_transition, EP_transition$challenge == 0) ### look at speeches given as dominant

EP_dominants <- EP_dominants %>%
  group_by(party_id) %>%
  mutate(change = min(date))

EP_dominants$running <- EP_dominants$date - EP_dominants$change 
range(EP_dominants$running)

EP_transition <- rbind(EP_challengers, EP_dominants)


############# Conduct RDD analyses for AE ######################################

rdd_AE <- RDestimate(as.numeric(embed_dict_scale) ~ as.numeric(running) , data = EP_transition, cutpoint = 0)
summary(rdd_AE)

table_data <- data.frame(
  Coefficient = rdd_AE$est,
  "Standard Error" = rdd_AE$se,
  Observations = rdd_AE$obs,
  p = rdd_AE$p,
  FEs = "None")

rdd_AE1 <- RDestimate(as.numeric(embed_dict_scale) ~ as.numeric(running) | party_id, data = EP_transition, cutpoint = 0)
summary(rdd_AE1)

table_data1 <- data.frame(
  Coefficient = rdd_AE1$est,
  "Standard Error" = rdd_AE1$se,
  Observations = rdd_AE1$obs,
  p = rdd_AE1$p,
  FEs = "Party")

rdd_AE2 <- RDestimate(as.numeric(embed_dict_scale) ~ as.numeric(running) | party_id + year, data = EP_transition, cutpoint = 0)
summary(rdd_AE2)

table_data2 <- data.frame(
  Coefficient = rdd_AE2$est,
  "Standard Error" = rdd_AE2$se,
  Observations = rdd_AE2$obs,
  p = rdd_AE2$p,
  FEs = "Party + Year")

tables_RDD <- rbind(table_data, table_data1, table_data2)

xtable(tables_RDD)

AE_RDD_plot <- rdrobust::rdplot(EP_transition$embed_dict_scale, 
                 as.numeric(EP_transition$running),  
                 c = 0,
                 kernel = "tri",
                 title = "Effect of Transformation from Challenger to Mainstream Party",
                 x.label = "Days before and after first speech as Mainstream Party ",
                 y.label =  "Anti-Establishment Rhetoric (standardized)",
                 x.lim = c(-365, 365),
                 ci = FALSE,
                 col.dots = "black",
                 col.lines = "red",
                 y.lim = c(-1,2),
                 p = 4)
ggsave(AE_RDD_plot$rdplot, file ="Figures_new/AE_rdd_02022024.png")



############# Conduct RDD analyses for Immigration ######################################

rdd_Immi <- RDestimate(as.numeric(immi_dict_scale) ~ as.numeric(running) , data = EP_transition, cutpoint = 0)
summary(rdd_Immi)

table_data <- data.frame(
  Coefficient = rdd_Immi$est,
  "Standard Error" = rdd_Immi$se,
  Observations = rdd_Immi$obs,
  p = rdd_Immi$p,
  FEs = "None")

rdd_Immi1 <- RDestimate(as.numeric(embed_dict_scale) ~ as.numeric(running) | party_id, data = EP_transition, cutpoint = 0)
summary(rdd_Immi1)

table_data1 <- data.frame(
  Coefficient = rdd_Immi1$est,
  "Standard Error" = rdd_Immi1$se,
  Observations = rdd_Immi1$obs,
  p = rdd_Immi1$p,
  FEs = "Party")

rdd_Immi2 <- RDestimate(as.numeric(embed_dict_scale) ~ as.numeric(running) | party_id + year, data = EP_transition, cutpoint = 0)
summary(rdd_Immi2)

table_data2 <- data.frame(
  Coefficient = rdd_Immi2$est,
  "Standard Error" = rdd_Immi2$se,
  Observations = rdd_Immi2$obs,
  p = rdd_Immi2$p,
  FEs = "Party + Year")

tables_RDD <- rbind(table_data, table_data1, table_data2)

xtable(tables_RDD)

Immi_RDD_plot <- rdrobust::rdplot(EP_transition$immi_dict_scale, 
                                as.numeric(EP_transition$running),  
                                c = 0,
                                kernel = "tri",
                                title = "Immigration",
                                x.label = "",
                                y.label =  "Immigration Emphasis (standardized)",
                                x.lim = c(-365, 365),
                                ci = FALSE,
                                col.dots = "black",
                                col.lines = "red",
                                y.lim = c(-1,2),
                                p = 4)


############# Conduct RDD analyses for Asuterity ######################################

rdd_Aut <- RDestimate(as.numeric(aut_dict_scale) ~ as.numeric(running) , data = EP_transition, cutpoint = 0)
summary(rdd_Aut)

table_data <- data.frame(
  Coefficient = rdd_Aut$est,
  "Standard Error" = rdd_Aut$se,
  Observations = rdd_Aut$obs,
  p = rdd_Aut$p,
  FEs = "None")

rdd_Aut1 <- RDestimate(as.numeric(aut_dict_scale) ~ as.numeric(running) | party_id, data = EP_transition, cutpoint = 0)
summary(rdd_Aut1)

table_data1 <- data.frame(
  Coefficient = rdd_Aut1$est,
  "Standard Error" = rdd_Aut1$se,
  Observations = rdd_Aut1$obs,
  p = rdd_Aut1$p,
  FEs = "Party")

rdd_Aut2 <- RDestimate(as.numeric(aut_dict_scale) ~ as.numeric(running) | party_id + year, data = EP_transition, cutpoint = 0)
summary(rdd_Aut2)

table_data2 <- data.frame(
  Coefficient = rdd_Aut2$est,
  "Standard Error" = rdd_Aut2$se,
  Observations = rdd_Aut2$obs,
  p = rdd_Aut2$p,
  FEs = "Party + Year")

tables_RDD <- rbind(table_data, table_data1, table_data2)

xtable(tables_RDD)

Aut_RDD_plot <- rdrobust::rdplot(EP_transition$aut_dict_scale, 
                                  as.numeric(EP_transition$running),  
                                  c = 0,
                                  kernel = "tri",
                                  title = "Austerity",
                                  x.label = "Days before and after first speech \n as Mainstream Party ",
                                  y.label =  "Austerity Emphasis (standardized)",
                                  x.lim = c(-365, 365),
                                  ci = FALSE,
                                  col.dots = "black",
                                  col.lines = "red",
                                  y.lim = c(-1,2),
                                  p = 4)


############# Conduct RDD analyses for EU ######################################

rdd_Int <- RDestimate(as.numeric(int_dict_scale) ~ as.numeric(running) , data = EP_transition, cutpoint = 0)
summary(rdd_Int)

table_data <- data.frame(
  Coefficient = rdd_Int$est,
  "Standard Error" = rdd_Int$se,
  Observations = rdd_Int$obs,
  p = rdd_Int$p,
  FEs = "None")

rdd_Int1 <- RDestimate(as.numeric(int_dict_scale) ~ as.numeric(running) | party_id, data = EP_transition, cutpoint = 0)
summary(rdd_Int1)

table_data1 <- data.frame(
  Coefficient = rdd_Int1$est,
  "Standard Error" = rdd_Int1$se,
  Observations = rdd_Int1$obs,
  p = rdd_Int1$p,
  FEs = "Party")

rdd_Int2 <- RDestimate(as.numeric(int_dict_scale) ~ as.numeric(running) | party_id + year, data = EP_transition, cutpoint = 0)
summary(rdd_Int2)

table_data2 <- data.frame(
  Coefficient = rdd_Int2$est,
  "Standard Error" = rdd_Int2$se,
  Observations = rdd_Int2$obs,
  p = rdd_Aut2$p,
  FEs = "Party + Year")

tables_RDD <- rbind(table_data, table_data1, table_data2)

xtable(tables_RDD)

Int_RDD_plot <- rdrobust::rdplot(EP_transition$int_dict_scale, 
                                 as.numeric(EP_transition$running),  
                                 c = 0,
                                 kernel = "tri",
                                 title = "EU-Integration",
                                 x.label = "",
                                 y.label =  "European Integration Emphasis (standardized)",
                                 x.lim = c(-365, 365),
                                 ci = FALSE,
                                 col.dots = "black",
                                 col.lines = "red",
                                 y.lim = c(-1,2),
                                 p = 4)
library(patchwork)
issue_RDDs <- Immi_RDD_plot$rdplot + Aut_RDD_plot$rdplot + Int_RDD_plot$rdplot
issue_RDDs
ggsave(issue_RDDs, file = "Figures/issue_rdds.png")
